import { Component, OnInit, EventEmitter, Input, Output, ElementRef } from '@angular/core';

import { InitService } from 'app/public/serve/init/init.service';
import { CudTableService } from 'app/public/serve/cud-table/cud-table.service';
import { HintsService } from 'app/public/serve/hints/hints.service';
import { Service } from '../../../server/server';
import { sysconfig } from '../sysconfig.data';
import { sysconfigUrl } from '../sysconfig.url';

declare var $: any;

@Component({
  selector: 'sysconfig-edit',
  templateUrl: './sysconfig-edit.component.html',
  styleUrls: ['./sysconfig-edit.component.css']
})
export class SysconfigEditComponent implements OnInit {

  @Output() onSubmit = new EventEmitter<void>();

  details: object = {};

  constructor(
    public initService: InitService,
    private request: CudTableService,
    public server: Service,
    private hintsService: HintsService
  ) { }

  ngOnInit() {
  }

  /**
   * 详情
   * @param info 
   */
  _details(info: object) {
    this.details = info;
  }

  /**
   * 修改
   * @param val 
   */ 
  _onSubmit(val) {
    val.sysStatus = Number(val.sysStatus);
    this.request.postRequest(this.server.baseUrl +sysconfigUrl.modify, val)
        .then((res: any)=> {
          if(res.result_code == '0000') {
            this.onSubmit.emit();
          }
          this.hintsService.messages(res.message, res.result_code);
          this._init();
          return
        })
        .catch((err: any)=> {
          this.hintsService.messages('error', 'error');
          this._init();
          return
        })
  }

  /**
   * 初始化弹窗
   */
  private _init() {
    this.initService._init();
    $('#edit').modal('hide');
    this.onSubmit.emit();
  }

}
